import axios from "axios";
import { VueAxios } from "./axios.js";

// 创建 axios 实例
const service = axios.create({
  baseURL: "/api", // api base_url
  timeout: 15000, // 请求超时时间
});
// console.log(process.env.VUE_APP_API_BASE_URL);
const err = (error) => {
  if (error.response) {
    const data = error.response.data;
    // const token = Vue.ls.get(ACCESS_TOKEN);
    if (error.response.status === 500) {
      notification.error({
        message: "服务器错误",
        description: data.message,
      });
    }
    if (error.response.status === 403) {
      notification.error({
        message: "Forbidden",
        description: data.message,
      });
    }
  }
  return Promise.reject(error);
};

// request interceptor
service.interceptors.request.use((config) => {
  debugger;
  // const token = Vue.ls.get(ACCESS_TOKEN);
  return config;
}, err);
service.interceptors.response.use((response) => {
  debugger;
  return response.data;
}, err);

const installer = {
  vm: {},
  install(Vue) {
    Vue.use(VueAxios, service);
  },
};

export { installer as VueAxios, service as axios };
